const common = require('../../utils/common')
var base64ToSrc = require("../../utils/base64src");
const { getShopProductPoster, shopProductSearch } = require('../../services/stores')
const defaultPage = {
  curPage: 1,
  pageSize: 10,
}
Page({
  data: {
    query: {
      ...defaultPage,
      keyword: "",
      orderType: '1', //1按销量倒序,2按价格升序,3按价格降序。
    },
    tabList: [
      { name: '销量', value: '1' },
      { name: '价格', value: '2' },
    ],
    currentTab: '1',
    total: 10,
    dataList: [],
    isSharePageShow: false,
    // 分享需要的图片
    sharePageInfo: {
      mainImage: '',
      codeUrl: '',
      address: "",
      price: '',
      shopName: "",
      title: ""
    },
    rotateAnimation: {
      data: null,
      animation: null,
      rotate: 0
    },
  },
  onLoad: function () {
    this.getDataList()
  },
  getDataList: function () {
    if (this.data.query.curPage > 1 && this.data.dataList.length >= this.data.total) return
    const params = { ...this.data.query }
    shopProductSearch(params).then((res) => {
      const { total } = res.page
      this.setData({ total })
      if (params.curPage === 1) {
        this.setData({
          dataList: res.data || []
        })
      } else {
        this.setData({
          dataList: [...this.data.dataList, ...res.data]
        })
      }
    }).catch(err => {
      common.log(err)
    })
  },
  tabChange: function (e) {
    const { value } = e.currentTarget.dataset;
    const currentTab = this.data.currentTab
    let tmpOrderType = this.data.query.orderType;
    if (currentTab == value) {
      if (currentTab != 1) {
        tmpOrderType = tmpOrderType == 2 ? 3 : 2;
        this.updateQuery({ orderType: tmpOrderType, ...defaultPage })
      }
    } else {
      this.updateQuery({ orderType: value, ...defaultPage })
    }
    this.setData({
      currentTab: value
    })
  },
  updateQuery: function (payload) {
    this.setData({
      query: { ...this.data.query, ...payload }
    }, this.getDataList)
  },
  scrollBottom: function () {
    const curPage = ++this.data.query.curPage
    this.updateQuery({ curPage })
  },
  onSearch: function (e) {
    const value = e.detail.value
    this.updateQuery({ keyword: value, ...defaultPage })
  },
  cancelSearch: function () {
    this.updateQuery({ keyword: '', ...defaultPage })
  },
  updateSharePage(payload) {
    this.setData({
      sharePageInfo: { ...this.data.sharePageInfo, ...payload }
    }, this.openSharePage)
  },
  //关闭海报页面
  closeSharePage: function () {
    this.setData({
      isSharePageShow: false,
    });
  },
  //打开海报页面
  openSharePage: function () {
    this.setData({
      isSharePageShow: true,
    });
  },
  //分享生成海报
  onSharePage: function (e) {
    const { index } = e.currentTarget.dataset;
    const item = this.data.dataList[index];
    if (!item.mainImage) {
      common.showToast("商品主图不存在!");
      return;
    }
    this.downloadSharePageImages(item);
  },

  //下载分享所需要的图片
  downloadSharePageImages: function (item) {
    const generateCodeData = {
      productId: item.id,
    }
    getShopProductPoster(generateCodeData).then((response) => {

      const { mainImage, codeUrl, price } = response.data
      Promise.all([common.wxDownloadFile(mainImage), base64ToSrc.imageUrlWithBase64src(codeUrl)]).then((res) => {
        const [tempMainImage, tempCodeUrl] = res
        const toFixedPrice = parseFloat(price).toFixed(2).toString()
        this.updateSharePage({ ...response.data, mainImage: tempMainImage, codeUrl: tempCodeUrl, price: toFixedPrice })
      }).catch(err => {
        common.log(err)
      })
    }).catch(err => {
      common.log(err)
    })
  },
  onPriceArrowRotate: function () {
    //动画
    if (!this.data.rotateAnimation.animation) {
      var tempRotateAnimation = this.data.rotateAnimation;
      var animation = wx.createAnimation({
        timingFunction: 'ease',
      });
      tempRotateAnimation.animation = animation;
      this.setData({
        rotateAnimation: tempRotateAnimation
      });
    }
    var tempRotateAnimation = this.data.rotateAnimation;
    var tempRotate = (tempRotateAnimation.rotate == -180) ? 0 : -180;
    tempRotateAnimation.rotate = tempRotate;
    tempRotateAnimation.animation.rotate(tempRotate).step();
    tempRotateAnimation.data = tempRotateAnimation.animation.export();
    this.setData({
      rotateAnimation: tempRotateAnimation
    });
  },
})
